package com.slkj.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.slkj.mapper.DocumentAllMapper;
import com.slkj.pojo.DocumentAll;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class DocumentAllService {
    @Resource
    private DocumentAllMapper documentAllMapper;


    //查询：主键
    public DocumentAll findDocumentById(Integer id){
        return  documentAllMapper.selectByPrimaryKey(id);
    }
    //查询：所有
    public List<DocumentAll> findAllDocument(){ return  documentAllMapper.selectAll(); }
    //增加
    public void addDocument(DocumentAll documentAll){
        documentAllMapper.insertSelective(documentAll);
    }
    //修改
    public void updateDocument(DocumentAll documentAll){
        documentAllMapper.updateByPrimaryKeySelective(documentAll);
    }

    //删除
    public void deleteDocument(DocumentAll documentAll) {
        documentAllMapper.deleteByPrimaryKey(documentAll);
    }

    //UUID查唯一
    public DocumentAll findByUUid(String uuid){
        Example example = new Example(DocumentAll.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isEmpty(uuid)){
           return null;
        }
        criteria.andEqualTo("docUuid",uuid);
        return  documentAllMapper.selectOneByExample(example);
    }



    //显示自己的收藏
    public  Page<DocumentAll> findByCollectAndName(String name,Integer page, Integer pageSize){
        System.out.println("Service>>name>>" + name + page + pageSize);
        PageHelper.startPage(page,pageSize);
        return (Page<DocumentAll>)documentAllMapper.findByCollectAndName(name);
    }

    //多条件
    public Page<DocumentAll> findListDocument(Map<String,String> searchMap, Integer page, Integer pageSize){
        PageHelper.startPage(page,pageSize);
        Example example = createExample(searchMap);
          List<DocumentAll> documentAlls = documentAllMapper.selectByExample(example);
          return (Page<DocumentAll>)documentAlls;
    }

    private Example createExample(Map<String, String> searchMap) {
        Example example = new Example(DocumentAll.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 1主键id
            if (searchMap.get("id") != null) {
                criteria.andEqualTo("id", searchMap.get("id"));
            }//2UUID
            if (searchMap.get("docUuid") != null) {
                criteria.andEqualTo("docUuid", searchMap.get("docUuid"));
            }  //3发送人
            if (searchMap.get("sendPeople") != null) {
                criteria.andEqualTo("sendPeople", searchMap.get("sendPeople"));
            }   //4文件状态
            if (searchMap.get("docStatus") != null) {
                criteria.andEqualTo("docStatus", searchMap.get("docStatus"));
            }
            //5文件标题
            if (searchMap.get("docTitle") != null) {
                criteria.andEqualTo("docTitle", searchMap.get("docTitle"));
            }
            // //6优先级别
            if (searchMap.get("speedLevel") != null) {
                criteria.andEqualTo("speedLevel", searchMap.get("speedLevel"));
            }   //7文件四大类型
            if (searchMap.get("docType") != null) {
                criteria.andEqualTo("docType", searchMap.get("docType"));
            }
            //9收件人编号数据,用来查
            if (searchMap.get("receivePeoplesNumArray") != null) {
                criteria.andLike("receivePeoplesNumArray", "%" + searchMap.get("receivePeoplesNumArray") + "%");
            }        //12抄送人
            if (searchMap.get("copyPeople") != null) {
                criteria.andEqualTo("copyPeople", searchMap.get("copyPeople"));
            }
            //11 是否回复
            if (searchMap.get("isReply") != null) {
                criteria.andEqualTo("isReply", searchMap.get("isReply"));
            }
            //8创建时间
            if (searchMap.get("addTime") != null) {
                criteria.andLike("addTime", "%" + searchMap.get("addTime") + "%");
            }
        }
        return example;
    }





}
